package sk.tuke.client;

import sk.tuke.client.components.buttons.ApplicationMenuButton;
import sk.tuke.client.components.buttons.CrossMainViewButton;
import sk.tuke.client.interfaces.InitInterface;
import sk.tuke.client.interfaces.LayoutFactory;
import sk.tuke.client.view.LoginView;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

public class ApplicationClickHandler implements ClickHandler, InitInterface {

	public void onClick(ClickEvent event) {
		if (authentication.isLogged()) {
			if (event.getSource() instanceof ApplicationMenuButton) {
				ApplicationMenuButton record = (ApplicationMenuButton) event.getSource();
				setContextLayoutView(record);
			} else if (event.getSource() instanceof CrossMainViewButton) {
				CrossMainViewButton record = (CrossMainViewButton) event.getSource();
				setMainLayoutView(record);
			} 
		} else
			mainLayout.setMembers(new LoginView());
	}

	private void setContextLayoutView(ApplicationMenuButton applicationMenuButton) {
		LayoutFactory factory = applicationMenuButton.getFactory();
		Canvas view = factory.create();
		southLayout.setMembers(view, westLayout);
	}

	private void setMainLayoutView(CrossMainViewButton applicationButton) {
		LayoutFactory factory = applicationButton.getFactory();
		factory.create();
	}

}